---
title: 'Template-Based Prompting'
description: 'Enhance prompt flexibility and reusability with dynamic templates and variables'
---

## What is Template-Based Prompting?

Template-based prompting is a technique that uses pre-defined structures with variable placeholders to create flexible, reusable prompts. These templates can be customized with different inputs while maintaining consistent structure and instructions, allowing you to scale prompt engineering efforts efficiently.

## Why Use Template-Based Prompting?

- **Consistency**: Ensure all prompts follow the same structure and maintain quality standards
- **Reusability**: Create prompts once and use them repeatedly with different inputs
- **Flexibility**: Adapt to various use cases without rewriting the entire prompt
- **Scalability**: Support large-scale applications with minimal maintenance
- **Reduced Engineering Effort**: Save time by modifying variables rather than crafting new prompts

## Basic Implementation in Latitude

Here's a simple template-based prompting example for content creation:

```markdown Content Creator
---
provider: OpenAI
model: gpt-4o
temperature: 0.7
---

# Professional Content Writer

You are a professional content writer specializing in {{ content_type }}.

## Task:
Create a {{ length }} piece about {{ topic }} in a {{ tone }} tone.

## Requirements:
{{ for point in key_points }}
- {{ point }}
{{ endfor }}

The content should be appropriate for {{ audience }} and include a catchy headline.
```

## Advanced Implementation with Conditional Logic

Let's create a more sophisticated example that uses PromptL's conditional capabilities:

<CodeGroup>
```markdown Advanced Template
---
provider: OpenAI
model: gpt-4o
temperature: 0.7
---

{{ content_type = content_type || "blog post" }}
{{ tone = tone || "professional" }}
{{ max_length = max_length || 500 }}

# Dynamic Content Generator

You are a professional writer specializing in {{ content_type }}.

## Style Guidelines:
{{ if tone == "formal" }}
Please use sophisticated vocabulary and business terminology.
{{ else if tone == "casual" }}
Please use conversational language and relatable examples.
{{ else }}
Please maintain a balanced, professional tone.
{{ endif }}

## Assignment:
Write about {{ topic }} in approximately {{ max_length }} words.

{{ if references }}
## References:
{{ for ref in references }}
- {{ ref }}
{{ endfor }}
{{ endif }}
```
</CodeGroup>

In this advanced example:
1. **Default Values**: We set fallbacks for missing parameters
2. **Conditional Logic**: Instructions adapt based on the specified tone
3. **Optional Sections**: Some parts only appear if certain parameters exist

## Template Components and Reusability

Modular templates with reusable components enhance scalability:

<CodeGroup>
```markdown Component-Based Template
---
provider: OpenAI
model: gpt-4o
temperature: 0.7
---

{{ if template_type === "content_creation" }}
  <prompt path="templates/content_creation" topic={{topic}} />
{{ endif }}

<step>
  You are a {{ role || "content writer" }} tasked with creating {{ content_type }}.

  {{ if instructions }}
    Special instructions:
    {{ instructions }}
  {{ endif }}
</step>

<user>
Please create {{ content_type }} about {{ topic }} that is {{ length || "500" }} words long.

{{ if key_points }}
  Include these key points:
  {{ for point in key_points }}
    - {{ point }}
  {{ endfor }}
{{ endif }}
</user>
```
</CodeGroup>

This pattern allows you to:
- Reference other templates with `<prompt path="..." />`
- Maintain a library of reusable components
- Create complex, multi-part prompts more easily

## Best Practices for Template-Based Prompting

<AccordionGroup>
<Accordion title="Template Design">
**Key Principles**:
- **Identify Variable Elements**: Mark parts of your prompt that will change frequently
- **Logical Structure**: Organize templates with clear sections and hierarchy
- **Documentation**: Include comments explaining required parameters
- **Naming Conventions**: Use consistent variable naming patterns

**Design Process**:
1. Start with a working static prompt
2. Identify components that might change between uses
3. Replace static elements with variables
4. Group related variables in meaningful sections
</Accordion>

<Accordion title="Variable Management">
**Variable Types**:
- **Required Variables**: Core parameters needed for the prompt to function
- **Optional Variables**: Additional parameters that enhance the prompt
- **Conditional Variables**: Parameters that trigger specific behaviors

**Best Practices**:
- Set default values for optional parameters
- Validate inputs when critical to prompt function
- Consider data types (text, numbers, lists, objects)
- Use descriptive names that indicate content and purpose
</Accordion>

<Accordion title="Template Testing">
**Testing Strategies**:
- Test with a variety of inputs to ensure robustness
- Validate edge cases (missing values, unusual inputs)
- Check conditional logic paths
- Use parameter combinations that test all template branches

**Quality Checks**:
- Ensure template renders correctly with minimal inputs
- Verify all conditional branches produce valid prompts
- Test with maximum expected values for lists and text
- Confirm error handling for missing critical variables
</Accordion>

<Accordion title="Maintenance & Governance">
**Maintaining Templates**:
- Version your templates to track changes
- Document parameter requirements clearly
- Create template libraries for related use cases
- Regularly review and update based on performance

**Organization**:
- Group related templates
- Create shared components for common elements
- Implement access controls for critical templates
- Document dependencies between templates
</Accordion>
</AccordionGroup>


## Advanced Techniques

### Template Versioning and A/B Testing

<CodeGroup>
```markdown A/B Test Templates
---
provider: OpenAI
model: gpt-4.1
temperature: 0.7
---

{{ template_version = test_group || "A" }}

{{ if template_version == "A" }}
<prompt path="templates/product_description_v1"
  product_name="{{ product_name }}"
  features="{{ features }}" />
{{ else }}
<prompt path="templates/product_description_v2"
  product_name="{{ product_name }}"
  features="{{ features }}" />
{{ endif }}
```
</CodeGroup>

## Integration with Other Techniques

Template-based prompting works well combined with other prompting techniques:

- **Few-Shot Learning + Templates**: Include examples that match the template pattern
- **Chain-of-Thought + Templates**: Structure reasoning steps with variable components
- **Self-Consistency + Templates**: Generate multiple responses using the same template
- **Role Prompting + Templates**: Define expert roles with customizable parameters

The key to effective integration is maintaining a balance between structure and flexibility.
